//
//  XBDRequest.m
//  Speedex
//
//  Created by speeda on 2016/10/31.
//  Copyright © 2016年 speeda. All rights reserved.
//

#import "AFHTTPSTool.h"
#import "XBDAES.h"
#import "AFNetworking/AFNetworking.h"

@interface AFHTTPSTool ()

@end

@implementation AFHTTPSTool


+(void)GETRequestRequestWebsite:(NSString *)str AndDictionary:(NSDictionary*)dict success:(void (^)(NSURLSessionDataTask * dataTask, id responseObject))success failure:(void (^)(NSURLSessionDataTask * dataTask, NSError * error))failure {
    NSString *s=[str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    //AES加密
    NSString *AESStr = [XBDAES AES128Encrypt:dict withKey:gkey];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"params"] = AESStr;
    //afnetworking的网络请求方式
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"text/html",@"text/json",@"image/jpeg",@"application/json", nil];//定义可以接受服务器返回的数据格式有哪些(content－type)

    [manager GET:s parameters:dic progress:nil success:success failure:failure];
    
}

+(void)POSTRequestRequestWebsite:(NSString *)str AndDictionary:(NSMutableDictionary*)dict success:(void (^)(NSURLSessionDataTask * dataTask, id responseObject))success failure:(void (^)(NSURLSessionDataTask * dataTask, NSError * error))failure {
    
    NSString *s=[str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    //AES加密
    NSString *AESStr = [XBDAES AES128Encrypt:dict withKey:gkey];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"params"] = AESStr;
    //afnetworking的网络请求方式
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"text/html",@"text/json",@"image/jpeg",@"application/json", nil];//定义可以接受服务器返回的数据格式有哪些(content－type)
    manager.requestSerializer.timeoutInterval = 20;
    
    [manager POST:s parameters:dic progress:nil success:success failure:failure];
}



- (AFSecurityPolicy *)customSecurityPolicy
{
    // /先导入证书
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"STAR.speeda.cn" ofType:@"cer"];//证书的路径
    NSData *certData = [NSData dataWithContentsOfFile:cerPath];
    
    // AFSSLPinningModeCertificate 使用证书验证模式
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    
    // allowInvalidCertificates 是否允许无效证书（也就是自建的证书），默认为NO
    // 如果是需要验证自建证书，需要设置为YES
    //securityPolicy.allowInvalidCertificates = YES;
    
    //validatesDomainName 是否需要验证域名，默认为YES；
    //假如证书的域名与你请求的域名不一致，需把该项设置为NO；如设成NO的话，即服务器使用其他可信任机构颁发的证书，也可以建立连接，这个非常危险，建议打开。
    //置为NO，主要用于这种情况：客户端请求的是子域名，而证书上的是另外一个域名。因为SSL证书上的域名是独立的，假如证书上注册的域名是www.google.com，那么mail.google.com是无法验证通过的；当然，有钱可以注册通配符的域名*.google.com，但这个还是比较贵的。
    //如置为NO，建议自己添加对应域名的校验逻辑。
    //securityPolicy.validatesDomainName = NO;
    
    securityPolicy.pinnedCertificates = [[NSSet alloc] initWithObjects:certData, nil,nil];
    
    return securityPolicy;
}

@end
